﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data; 
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MKIB3Game;
using System.Threading; 

namespace GuiConny
{
    class MapCanvas : Canvas
    { 
        private BitmapImage _image;
        private BitmapImage _player;
        private BitmapImage _dragon;
        private BitmapImage _FOREST;
        private BitmapImage _WATER;
        private BitmapImage _WALL;
        private BitmapImage _WALKABLE;
        private BitmapImage _HUNTABLE;
        private BitmapImage _BACKGROUND;
        private BitmapImage _selection;
         

        private Map map;
        private MapCell stackCell;
        private List<Figure> dragons=null;
        public List<Figure> players=null;
        private List<MapCellPropertyEnum> stackList;
        private Random rnd = new Random();
        public List<Point> selection = new List<Point>();

        public static int FIELD_SIZE = 50;
        public static int FIELD_MARGIN = 0;
        public static int MOVE_TEXT_Y = 7;
        public static int MOVE_TEXT_X = 0;
        public static bool DRAW_MAP = true;

        private Figure moveP;
        private DrawingContext moveDC;

        
        public MapCanvas()
         {
                _image =    new BitmapImage(new Uri("../../images/icon.jpg", UriKind.Relative));
                _selection = new BitmapImage(new Uri("../../images/selection.png", UriKind.Relative));
               _player =    new BitmapImage(new Uri("../../images/player.png", UriKind.Relative));
               _dragon =    new BitmapImage(new Uri("../../images/dragon.png", UriKind.Relative));
               _FOREST = new BitmapImage(new Uri("../../images/forest.png", UriKind.Relative));
               _WATER =     new BitmapImage(new Uri("../../images/water.png", UriKind.Relative));
               _WALL = new BitmapImage(new Uri("../../images/Textures/Belshire_sc2/wall.png", UriKind.Relative));
               _WALKABLE = new BitmapImage(new Uri("../../images/walkable.png", UriKind.Relative));
               _HUNTABLE = new BitmapImage(new Uri("../../images/huntable.png", UriKind.Relative));
               _BACKGROUND = new BitmapImage(new Uri("../../images/background.png", UriKind.Relative));
             
               stackList = new List<MapCellPropertyEnum>(); 
        }    
    
        //----------------- SETTER------------------------

        public void setMap(Map map){
            this.map = map;
        }
        
        public void setPlayers(List<Figure> p)
        {
            this.players = p;
        }
        
        public void setDragons(List<Figure> d)
        {
            this.dragons = d;
        }

        // ---------------------- Render-Function, called whenever anything needs to be updated, has been invalidated

        protected override void OnRender(DrawingContext dc)
        {
            String infoMessage="";
            //TESTMETHODE
            if (map == null)
            {
                infoMessage += "No map loaded yet ";
                //map = Toolbox.generateMap(20, 30);
            }
            if (players == null)
            {
                infoMessage += "No player loaded yet ";
                //player = Toolbox.generatePlayers(8, 10);
            }
            if (dragons == null)
            {
                infoMessage += "No Dragons loaded yet";
                //dragons = Toolbox.generateDragons(8, 15);
            }
            //TESTMETHODE 
            
            if (map != null)
            {
                int x = map.getWidth();
                int y = map.getHeigth();
                if (DRAW_MAP)
                {
                    for (int i = 0; i < x; i++)
                    {
                        for (int j = 0; j < y; j++)
                        {
                            drawField(i, j, dc);
                        }
                    }
                    this.Width = x * FIELD_SIZE + FIELD_MARGIN;
                    this.Height = y * FIELD_SIZE + FIELD_MARGIN;
                }// end drawmap


                // dragons and player
                if (players != null)
                {
                    foreach (Figure p in players)
                    {
                        drawFigure(p, dc);
                        // movePlayer(p, dc);
                    }
                }
                
                if (dragons != null)
                {
                    foreach (Figure d in dragons)
                    {
                        drawFigure(d, dc);
                    }
                }
            }// ende map !=null
          
            if (!infoMessage.Equals(""))
            {
                FormattedText ft = new FormattedText(infoMessage, System.Globalization.CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Klavika"), 20, System.Windows.Media.Brushes.Black);
                dc.DrawText(ft, new System.Windows.Point(0, 0));
            } 
            drawSelection(dc); 
        }

        private void drawSelection(DrawingContext dc)
        {
            foreach( Point p in this.selection)
            {
                dc.DrawImage(_selection,
              new Rect(p.X * FIELD_SIZE + FIELD_MARGIN, p.Y * FIELD_SIZE + FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN));
            }

        }

        private void drawFigure(Figure p, DrawingContext dc)
        {
            int x = p.getX();
            int y = p.getY();
            this.drawField(x, y, dc);
            if (p.getType() == FigureTypeEnum.PLAYER)
            {
                dc.DrawImage(_player,
                new Rect(x * FIELD_SIZE + FIELD_MARGIN, y * FIELD_SIZE + FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN));
            }
            if (p.getType() == FigureTypeEnum.DRAGON)
            {
                dc.DrawImage(_dragon,
                new Rect(x * FIELD_SIZE + FIELD_MARGIN, y * FIELD_SIZE + FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN));
            }

            string name = p.getDesc();
            FormattedText ft = new FormattedText(name, System.Globalization.CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Klavika"), 13, System.Windows.Media.Brushes.White);
            dc.DrawText(ft, new System.Windows.Point(x * FIELD_SIZE + FIELD_MARGIN + MOVE_TEXT_X, y * FIELD_SIZE + FIELD_MARGIN - MOVE_TEXT_Y));
        }

        private void drawField(int x, int y, DrawingContext dc)
        {

            stackCell = map.getMapCell(x, y);
            stackList = stackCell.getCellPropertyArrayList();

            // draw botton up --> FOREST --> WALL --> WATER --> WALKABLE --> HUNTABLE 
            dc.DrawImage(_BACKGROUND,
             new Rect(x * FIELD_SIZE + FIELD_MARGIN, y * FIELD_SIZE + FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN));

            if (stackList.Contains(MapCellPropertyEnum.FOREST))
                dc.DrawImage(_FOREST,
               new Rect(x * FIELD_SIZE + FIELD_MARGIN, y * FIELD_SIZE + FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN));
            if (stackList.Contains(MapCellPropertyEnum.WALL))
                dc.DrawImage(_WALL,
               new Rect(x * FIELD_SIZE + FIELD_MARGIN, y * FIELD_SIZE + FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN));
            if (stackList.Contains(MapCellPropertyEnum.WATER))
                dc.DrawImage(_WATER,
               new Rect(x * FIELD_SIZE + FIELD_MARGIN, y * FIELD_SIZE + FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN));
            if (stackList.Contains(MapCellPropertyEnum.WALKABLE))
                dc.DrawImage(_WALKABLE,
               new Rect(x * FIELD_SIZE + FIELD_MARGIN, y * FIELD_SIZE + FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN));
            if (stackList.Contains(MapCellPropertyEnum.HUNTABLE))
                dc.DrawImage(_HUNTABLE,
               new Rect(x * FIELD_SIZE + FIELD_MARGIN, y * FIELD_SIZE + FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN));
        }// end draw Field
        
        //---------------- Nice to have Functions, dont work yet -----------------------------
        
        private void movePlayer(Figure p, DrawingContext dc)
        {

            Console.WriteLine("init");
            this.moveP = p;
            this.moveDC = dc;
            //Thread movethread = new Thread(movePl);
           // movethread.Name = "movethread";
           // movethread.Start();
            movePl();
            Console.WriteLine("moving");
        }
        
        private void movePl()
        {
            Console.WriteLine("movePl");
           
                Console.WriteLine("last pained move count >0");
                int lastX;
                int lastY; 
             
                int x = moveP.getX() ;
                int y = moveP.getY() ;

                if (moveP.lastPaintedMove.Count < 1)
                {
                    lastX = x;
                    lastY = y;
                }
                else
                {
                    lastX  = ((int)moveP.lastPaintedMove[moveP.lastPaintedMove.Count - 1].X) ;
                    lastY = ((int)moveP.lastPaintedMove[moveP.lastPaintedMove.Count - 1].Y) ;
                }

                int moveDirectionX = x - lastX; // neu minus alt
                int moveDirectionY = y - lastY;
                int STEP_SIZE = 10;

                for (int i = 0; i < FIELD_SIZE; i = i + STEP_SIZE)
                {
                    Console.WriteLine("Move step: " + i);
                
                    //this.drawField(x, y, moveDC);
                    if (moveP.getType() == FigureTypeEnum.PLAYER)
                    {
                        moveDC.DrawImage(_player,
                        new Rect(lastX * FIELD_SIZE + i + FIELD_MARGIN, lastY * FIELD_SIZE + i + FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN));
                    }
                    if (moveP.getType() == FigureTypeEnum.DRAGON)
                    {
                        moveDC.DrawImage(_dragon,
                        new Rect(lastX * FIELD_SIZE + i + FIELD_MARGIN, lastY * FIELD_SIZE + i + FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN, FIELD_SIZE - FIELD_MARGIN));
                    }

                    string name = moveP.getDesc();
                    FormattedText ft = new FormattedText(name, System.Globalization.CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Klavika"), 13, System.Windows.Media.Brushes.White);
                    moveDC.DrawText(ft, new System.Windows.Point(lastX * FIELD_SIZE + i + FIELD_MARGIN + MOVE_TEXT_X, y * FIELD_SIZE + FIELD_MARGIN - MOVE_TEXT_Y));
                    Console.WriteLine("Move step end: " + i);
                    Thread.Sleep(100);
                }// ende for                  
            
        }

          
    }//end class
}
